package com.ksv.qlsv;

import com.ksv.lop.*;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.database.Cursor;
import android.database.sqlite.SQLiteCursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class KhoaActivity extends Activity {
    /** Called when the activity is first created. */
	private GridView gv_khoa;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frmkhoa);   
        this.setTitle(getResources().getString(R.string.khoa));
        gv_khoa = (GridView)findViewById(R.id.gv_khoa);
        onItemClickGridView();
        loadListKhoa();
    }
    
    @Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
    	MenuInflater inf = getMenuInflater();
    	inf.inflate(R.menu.contextmenu_update, menu);
		super.onCreateContextMenu(menu, v, menuInfo);
	}
    
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		return super.onContextItemSelected(item);
	}

	private void onItemClickGridView(){
    	try{
    		gv_khoa.setOnItemClickListener(new OnItemClickListener(){
        	@Override
			public void onItemClick(AdapterView<?> parent, View v, int position,long id){
				// TODO Auto-generated method stub
        		try
				{
					SQLiteCursor cr = (SQLiteCursor)parent.getItemAtPosition(position);
					String sTenKhoa = cr.getString(cr.getColumnIndex(ConnectDB.tenkhoa));
					int iMaKhoa = cr.getInt(cr.getColumnIndex(ConnectDB.makhoa));
					Khoa kh = new Khoa();
					kh.setMaKhoa(iMaKhoa);
					kh.setTenKhoa(sTenKhoa);
					AlertDialog diag = Alerts.ShowEditDialog_Khoa(KhoaActivity.this, kh);
					diag.setOnDismissListener(new OnDismissListener() {
						
						@Override
						public void onDismiss(DialogInterface dialog) {
							// TODO Auto-generated method stub
							loadListKhoa();
						}
					});
					diag.show();
				}
				catch(Exception ex)
				{
					Alerts.CatchError(KhoaActivity.this, ex.toString());
				}
			}			
        }
        );
        }
        catch(Exception ex)
        {
        	
        }
    }
    
    private void loadListKhoa(){
    	try{
    	    Cursor c = ConnectDB.getAllKhoa();
    	    startManagingCursor(c);
    	    String [] from = new String []{ConnectDB.makhoa, ConnectDB.tenkhoa};
    		int [] to = new int [] {R.id.col_ma_khoa, R.id.col_ten_khoa};
    		SimpleCursorAdapter sca = new SimpleCursorAdapter(this,R.layout.frmkhoa_gridrow,c,from,to);
    		gv_khoa = (GridView)findViewById(R.id.gv_khoa);
    		gv_khoa.setAdapter(sca);
    	}
    	catch(Exception ex){
    		
    	}
    }

}
